/* 
 * File:   SimulationThread.h
 * Author: jcrada
 *
 * Created on April 11, 2009, 11:47 AM
 */

#ifndef _SIMULATIONTHREAD_H
#define	_SIMULATIONTHREAD_H

#include "BulletSimulator.h"

#include <QtCore/QThread>
#include <QtCore/QTime>
#include <string>

namespace jcrada {

    class SimulationThread : public QThread {
        Q_OBJECT
    signals:
        virtual void started();
        virtual void stopped();
        virtual void message(const QString& message);
    private:
        bool _stopped;
        int _simulation_speed;
        BulletSimulator* _simulator;
        int _iterations;
        QTime* _timer;
        volatile bool _behavior_enabled;
        volatile bool _behavior_on_ground;
        int _current_iteration;
    protected:
        virtual void performStep();
        virtual void ensureFrameRate(int time_elapsed_in_milliseconds);

        virtual void setStopped(bool stopped) {
            this->_stopped = stopped;
        }

        virtual QTime& getTimer() const {
            return *this->_timer;
        }


    public:
        SimulationThread(BulletSimulator& simulator = BulletSimulator::getSingleton());
        virtual ~SimulationThread();

        virtual void run();

        virtual void play();
        virtual void stop();

        virtual void iterate() = 0;

        virtual bool isStopped() const {
            return this->_stopped;
        }

        virtual void setSimulationSpeed(int speed) {
            this->_simulation_speed = speed;
        }

        virtual int getSimulationSpeed() const {
            return this->_simulation_speed;
        }

        virtual BulletSimulator& getSimulator() const {
            return *this->_simulator;
        }

        virtual void setIterations(int iterations) {
            this->_iterations = iterations;
        }

        virtual int getIterations() const {
            return this->_iterations;
        }

        virtual void setBehaviorEnabled(bool enabled) {
            this->_behavior_enabled = enabled;
        }

        virtual bool isBehaviorEnabled() const {
            return this->_behavior_enabled;
        }

        virtual void setBehaviorOnGround(bool on_ground) {
            this->_behavior_on_ground = on_ground;
        }

        virtual bool isBehaviorOnGround() const {
            return this->_behavior_on_ground;
        }

        virtual void setCurrentIteration(int iteration) {
            this->_current_iteration = iteration;
        }

        virtual int getCurrentIteration() const {
            return this->_current_iteration;
        }
    };
}

#endif	/* _SIMULATIONTHREAD_H */

